// module_activity/pages/activity_list/detail/detail.js
import Toast from "../../../../miniprogram_npm/@vant/weapp/toast/toast"
import Dialog from "../../../../miniprogram_npm/@vant/weapp/dialog/dialog"
const app = getApp()
const http = new app.appHttp()
const log = new app.appLogs()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    level: -1,
    recordId: null,
    //活动详情
    draw: null,
    //中奖项
    award: 1,
    mode: 2, // 旋转模式
    isShow: false,
    isZJ: false,
    isWZJ: false,
    winAwardHeight: 204, //我的奖品高度 单位rpx
    isFinish: true,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getData()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /**
   * 再来一次
   */
  onNextWheelStart: function () {
    this.onClose()
    setTimeout(() => {
      this.wheelStart()
    },300)
  },

  // 用户点击开始抽奖
  wheelStart() {
    if(!this.data.isFinish) {
      return
    }else {
      this.data.isFinish = false
    }
    let wzj = [2,4,6]
    let award = 2
    let than = this
    Toast.loading({
      duration: 0,
      message: "加载中..."
    })
    http.post("draw/start",{
      draw_id: than.data.id
    }).then(res => {
      Toast.clear()
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.level == 0) {
          let xb = Math.floor(Math.random() * 2)
          award = wzj[xb]
        }else {
          switch (res.data.data.level) {
            case 1:
              award = 1
              break
            case 2:
              award = 3
              break
            case 3:
              award = 5
              break
          }
        }
        than.setData({
          recordId: res.data.data.record_id,
          level: res.data.data.level,
          award: award
        })
        // 触发组件开始方法
        than.selectComponent('#sol-wheel').begin()
      }else {
        this.data.isFinish = true
        Dialog.alert({
          title: "抽奖失败",
          message: res.data.msg
        }).then(() => {  })
      }
    }, err => {
      this.data.isFinish = true
      Toast.clear()
      Dialog.alert({
        title: "抽奖失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {  })
    })
  },
  // 抽奖完成后操作
  wheelSuccess() {
    const _this = this
    const data = this.data
    const index = this.data.award - 1
    setTimeout(function () {
      if (_this.data.level > 0) {
        _this.setData({
          isZJ: true
        })
      }else {
        _this.setData({
          isWZJ: true
        })
      }
    }, 300);
    setTimeout(() => {
      _this.data.isFinish = true
    },500)
  },

  /**
   * 关闭弹出窗
   * @param e
   */
  onClose: function (e) {
    this.setData({
      isShow: false,
      isZJ: false,
      isWZJ: false
    })
    this.getData()
  },

  /**
   * 奖品详情
   * @param e
   */
  onPrize: function (e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: "./prize/prize?id=" + id
    })
  },

  /**
   * 获取抽奖活动详情
   */
  getData: function () {
    http.get("draw/detail",{
      draw_id: this.data.id,
      user_id: app.globalData.userInfo.applet_user_id
    }).then(res => {
      if (res.data.code === http.REQUEST_OK) {
        if (res.data.data.win_award.length > 1) {
          this.data.winAwardHeight = 204 + (res.data.data.win_award.length - 1) * 152
        }
        res.data.data.start_time = res.data.data.start_time.substring(0,10)
        res.data.data.end_time = res.data.data.end_time.substring(0,10)
        this.setData({
          winAwardHeight: this.data.winAwardHeight,
          draw: res.data.data
        })
      }else {
        Dialog.alert({
          title: "加载失败",
          message: res.data.msg
        }).then(() => {
          wx.navigateBack()
        })
      }
    }, err => {
      Dialog.alert({
        title: "加载失败",
        message: "网络异常，请稍后再试！"
      }).then(() => {
        wx.navigateBack()
      })
    })
  },

})